<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise API</title>
</head>
<body>
    <script>
        //
        let p = new Promise((resolve, reject) => {
            // ** 同步调用
            console.log(111);
            //修改 promise 对象的状态
            reject('error test');
        });

        // console.log(222);

        p.then(value => {
            console.log('value1') ;
        }, error => {
            // 捕获错误后 后面的catch  还能捕获错误
            console.log('error => ', error) ;
            console.log(p) ;
            
        })
        .then(value => {
            sleep(5000)
            console.log('value2', value) ;
        }, error => {
            console.log('error2 => ', error) ;
        }).catch(reason => {
            console.log('这里并没有错误') ;
            console.warn(reason);
        });



        //执行 catch 方法
        p.catch(reason => {
            sleep(1000)

            console.log('------- catch----------------\n') ;
            console.log(reason);
        }).then(value => {
            sleep(2000)
            console.log('catch value2', value) ;
        }, error => {
            console.log('catch error2 => ', error) ;
        });


       p.then(value => {
            console.log('value2') ;
        }, error => {
            sleep(100)
            console.log('-----error2------------------\n') ;
            console.log('error2 => ', error) ;
        })



       function sleep(ms) {
            var start = Date.now()
            while(Date.now() - start <= ms ) ; 
       }
    </script>
</body>
</html>